package com.zzvcom.webapp.action.web;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.mail.GmailSender;
import com.zzvcom.mail.SendParam;
import com.zzvcom.persist.model.admin.Config;
import com.zzvcom.persist.model.admin.Inquiry;
import com.zzvcom.persist.model.admin.Subscription;
import com.zzvcom.service.admin.ConfigManager;
import com.zzvcom.service.admin.InquiryManager;
import com.zzvcom.service.admin.SubscriptionManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.DateUtil;

@Controller
@Scope("prototype")
public class InquiryViewAction
{

    private Inquiry inquiry;

    private InquiryManager manager;
    
    private SubscriptionManager subManager;

    private ConfigManager cm;

    private ResultPage resultPage;

    private String param;
    
    private Subscription subs;

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        if (StringUtils.isNotBlank(param))
        {
            resultPage.setCurrentPage(Integer.valueOf(param));
        }
        DetachedCriteria dc = DetachedCriteria.forClass(Inquiry.class);
        dc.add(Restrictions.eq("display", "0"));
        dc.addOrder(Order.desc("addtime"));
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        return "inquiry";
    }

    public String p()
    {
        return execute();
    }

    public void add()
    {
        if (inquiry == null || StringUtils.isBlank(inquiry.getEmail()))
        {
            return ;
        }
        inquiry.setAddtime(DateUtil.dateToNum14(new Date()));
        inquiry.setDisplay("1");
        inquiry.setIsnew("0");
        manager.save(inquiry);
        sendMail();
        
    }
    
    public void addSubscription()
    {
        if(subs!=null && StringUtils.isNotBlank(subs.getEmail()))
        {
            subs.setId(null);
            subs.setAddtime(DateUtil.dateToNum14(new Date()));
            subManager.save(subs);
        }
         
    }

    private void sendMail()
    {
        Config c = cm.getConfig();
        StringBuilder sb = new StringBuilder();
        sb.append("询盘人邮件：");
        sb.append(inquiry.getEmail());
        sb.append("<br/>");

        sb.append("询盘时间：");
        sb.append(inquiry.getAddtime());
        sb.append("<br/>");

        sb.append("称呼：");
        sb.append(inquiry.getName());
        sb.append("<br/>");

        sb.append("国家：");
        sb.append(inquiry.getCountry());
        sb.append("<br/>");
        
        sb.append("单位：");
        sb.append(inquiry.getCompany());
        sb.append("<br/>");        
        
        sb.append("联系地址：");
        sb.append(inquiry.getAddress());
        sb.append("<br/>");    
        
        sb.append("TEL：");
        sb.append(inquiry.getTel());
        sb.append("<br/>");            

        sb.append("<font color=red>询盘内容：</font><br/>");
        sb.append(inquiry.getMessages());

        SendParam param = new SendParam();

        param.setUsername(c.getMailaccount());
        param.setPassword(c.getMailpass());
        param.setSubject(c.getPresubject() + inquiry.getAddtime());
        param.setText(sb.toString());
        param.setTo(c.getMailto());
        
        GmailSender gs=new GmailSender();
        gs.send(param);

    }

    public String a()
    {
        inquiry = manager.get(param);
        return "ainquiry";
    }

    public Inquiry getInquiry()
    {
        return inquiry;
    }

    public void setInquiry(Inquiry inquiry)
    {
        this.inquiry = inquiry;
    }

    public InquiryManager getManager()
    {
        return manager;
    }

    public ConfigManager getCm()
    {
        return cm;
    }

    @Autowired
    public void setCm(ConfigManager cm)
    {
        this.cm = cm;
    }

    @Autowired
    public void setManager(InquiryManager manager)
    {
        this.manager = manager;
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public String getParam()
    {
        return param;
    }

    public void setParam(String param)
    {
        this.param = param;
    }

    public SubscriptionManager getSubManager()
    {
        return subManager;
    }
    @Autowired
    public void setSubManager(SubscriptionManager subManager)
    {
        this.subManager = subManager;
    }

    public Subscription getSubs()
    {
        return subs;
    }

    public void setSubs(Subscription subs)
    {
        this.subs = subs;
    }

}
